<template>
  <div class="login">
    <!-- 导航栏
    van-nav-bar
        title:"中间标题"
        left-text:左边文本
        right-text:右边文本
        left-arrow左边剪头
        @click-left:左边点击
        @click-right:右边点击
        slot="title/left/right"    
     -->
    <van-nav-bar>
      <template #left>
        <i class="iconfont f44">&#xe637;</i>
      </template>
    </van-nav-bar>
    <div class="content">
      <h3 class="login-title">您好，请登录</h3>
      <!-- 表单验证
      1:van-field定义一个rules
         rules规则:[
         {
             required:true,message:错误信息.trigger:"onChange/onBlur"
         },
         {
             validator:(value,rule)=>{
                 return boolean
                   true:验证通过
                   false:验证失败
             },
             message:"错误信息",
             trigger:"onChange/onBlur"
         }
         ]
    2:全局表单验证
       1:在van-form定义一个ref  ref=值
       2:this.$refs.ref值.validate().then(()=>{
           // 验证通过
       }).catch(()=>{
           // 验证失败
       })
    3:局部表单验证
       1:van-filed上加一个name
       2:
       this.$refs.ref值.validate(name的值).then(()=>{
           // 验证通过
       }).catch(()=>{
           // 验证失败
       })
       -->
      <van-form ref="form">
        <van-field
          :rules="rules.mobile"
          v-model="form.mobile"
          name="mobile"
          placeholder="请输入手机号"
        >
          <template #left-icon>
            <i class="iconfont r20">&#xe60b;</i>
          </template>
        </van-field>
        <van-field
          :rules="rules.code"
          v-model="form.code"
          placeholder="请输入验证码"
        >
          <template #left-icon>
            <i class="iconfont r20">&#xe60c;</i>
          </template>
          <template #button>
            <!-- totalTime===6 -->
            <span class="code" v-if="totalTime === 6" @click="getCode"
              >获取验证码</span
            >
            <span class="code" v-else>{{ totalTime }}后重试</span>
          </template>
        </van-field>
      </van-form>
      <div class="tip">
        登录即同意<span class="color">《用户使用协议》</span>和<span
          class="color"
          >《隐私协议》</span
        >
      </div>
      <van-button block round color="#e40137" @click="login">确定</van-button>
    </div>
  </div>
</template>
<script>
import { auCode, auLogin } from '@/api/login.js'
import { setLocal } from '@/utils/local.js'
export default {
  data () {
    return {
      totalTime: 6, // 倒计时时间
      form: {
        mobile: '', // 是string手机号
        code: '' // 是string验证码
      },
      rules: {
        mobile: [
          { required: true, message: '请输入手机号', trigger: 'onChange' },
          {
            validator: value => {
              return /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/.test(
                value
              )
            },
            message: '请正确输入手机号',
            trigger: 'onChange'
          }
        ],
        code: [{ required: true, message: '请输入验证码', trigger: 'onChange' }]
      }
    }
  },
  created () {
    // alert(process.env.VUE_APP_URL)
  },
  methods: {
    //   获取验证码111
    getCode () {
      this.$refs.form
        .validate('mobile')
        .then(() => {
          // 倒计时处理
          // 解决刚开始一秒按钮还可以点击的问题
          this.totalTime--
          let _interval = setInterval(() => {
            this.totalTime--
            if (this.totalTime <= 0) {
              // 清除倒计时,让时间恢复
              clearInterval(_interval)
              this.totalTime = 6
            }
          }, 1000)
          auCode({
            mobile: this.form.mobile
          }).then(res => {
            this.$toast.success(res.data.data)
          })
        })
        .catch(() => {
          this.$toast.fail('验证失败')
        })
    },
    // 登陆
    login () {
      this.$refs.form
        .validate()
        .then(() => {
          auLogin(this.form).then(res => {
            window.console.log(res)
            // 保存token
            setLocal('token', res.data.data.jwt)
            // 存储用户信息
            this.$store.commit('setUserInfo', res.data.data.user)
            // 设置用户登陆状态
            this.$store.commit('setIsLogin', true)
            // 跳转到home页
            this.$router.push('/home')
          })
          // this.$toast.success('验证成功')
        })
        .catch(() => {
          this.$toast.fail('验证失败')
        })
    }
  }
}
</script>
<style lang="less" scoped>
.login {
  .f44 {
    font-size: 44px;
  }
  &::v-deep .van-nav-bar__left,
  &::v-deep .van-nav-bar__right {
    padding: 0;
  }
  .content {
    padding: 0 @p15;
    .login-title {
      margin: 50px 0 65px;
    }
    .r20 {
      padding-right: 20px;
    }
    .code {
      font-size: 16px;
      font-family: PingFangSC, PingFangSC-Regular;
      font-weight: 400;
      text-align: center;
      color: #00b8d4;
      line-height: 22px;
      letter-spacing: 0px;
    }
    .tip {
      font-size: 12px;
      margin-top: 27px;
      margin-bottom: 44px;
      .color {
        color: #00b8d4;
      }
    }
  }
}
</style>
